package com.cloud.mkz.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.mkz.admin.api.util.TreeUtil;
import com.cloud.mkz.core.common.util.ResponseResult;
import com.cloud.mkz.admin.api.entity.RbacMenu;
import com.cloud.mkz.admin.service.RbacMenuService;
import com.cloud.mkz.core.mvc.controller.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 权限信息
 *
 * @author mkz code generator
 * @date 2021-03-20 20:23:13
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/rbac/menu" )
@Api(value = "rbacmenu", tags = "权限信息管理")
public class RbacMenuController extends BaseController {
    private final  RbacMenuService rbacMenuService;

    /**
     * @param rbacMenu 菜单信息
     * @return ResponseResult
     */
    @ApiOperation(value = "登录后菜单", notes = "分页查询")
    @GetMapping("/loginTree" )
    public Map<String, Object> loginTree(RbacMenu rbacMenu) {
        Map<String, Object> resultMap = new HashMap<>(16);
        Map<String,Object> home = new HashMap<>(16);
        Map<String,Object> logo = new HashMap<>(16);
        List<RbacMenu> list = super.getMenuTree();
        resultMap.put("menuInfo", TreeUtil.getTreeInfo(list, "-9"));
        home.put("title","首页");
        home.put("href","page/welcome-1.html?t=2");
        logo.put("title","MKZ-UI");
        //设置Logo
        logo.put("image","images/logo.png");
        resultMap.put("homeInfo", home);
        resultMap.put("logoInfo", logo);
        return resultMap;
    }

    /**
     * @param rbacMenu 菜单信息
     * @return ResponseResult
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/main" )
    public ResponseResult rbacMenuMain(RbacMenu rbacMenu) {
        QueryWrapper<RbacMenu> wrapper = Wrappers.query(rbacMenu);
        wrapper.orderByAsc("parent_id").orderByAsc("order_no");
        return ResponseResult.ok(rbacMenuService.list(wrapper));
    }

    /**
     * @return ResponseResult
     */
    @ApiOperation(value = "查询角色对应权限树", notes = "查询")
    @GetMapping("/tree/{roleId}" )
    public ResponseResult roleTree(@PathVariable("roleId" ) Long roleId) {
        List<RbacMenu> list = rbacMenuService.getTreeMenu(roleId);
        return ResponseResult.ok(TreeUtil.buildTree(list, "-9"));
    }

    /**
     * @return ResponseResult
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/tree" )
    public ResponseResult rbacMenuTree() {
        RbacMenu rbacMenu = new RbacMenu();
        QueryWrapper<RbacMenu> wrapper = Wrappers.query(rbacMenu);
        wrapper.eq("menu_type", 1).or().eq("menu_type", 2);
        wrapper.orderByAsc("parent_id").orderByAsc("order_no");
        List<RbacMenu> result = rbacMenuService.list(wrapper);
        rbacMenu.setDisplayName("根目录").setParentId(-1L).setPkId(-9L);
        result.add(0, rbacMenu);
        return ResponseResult.ok(result);
    }

    /**
     * 分页查询
     * @param page 分页对象
     * @param rbacMenu 权限信息
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page" )
    @PreAuthorize("@pms.hasPermission('admin_rbacmenu_get')" )
    public ResponseResult getRbacMenuPage(Page page, RbacMenu rbacMenu) {
        return ResponseResult.ok(rbacMenuService.page(page, Wrappers.query(rbacMenu)));
    }


    /**
     * 通过id查询权限信息
     * @param pkId id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{pkId}" )
    public ResponseResult getById(@PathVariable("pkId" ) Long pkId) {
        return ResponseResult.ok(rbacMenuService.getInfoById(pkId));
    }

    /**
     * 新增权限信息
     * @param rbacMenu 权限信息
     * @return R
     */
    @ApiOperation(value = "新增权限信息", notes = "新增权限信息")
    @PostMapping
    public ResponseResult save(@RequestBody RbacMenu rbacMenu) {
        RbacMenu tempMenu = new RbacMenu();
        if(rbacMenu.getMenuType()==2){
            tempMenu.setMenuUrl(rbacMenu.getMenuUrl());
        }else if(rbacMenu.getMenuType()==3){
            tempMenu.setAuthorityKey(rbacMenu.getAuthorityKey());
        }
        List<RbacMenu> tempList = rbacMenuService.list(Wrappers.query(tempMenu));
        if(tempList.size()>0){
            if(rbacMenu.getMenuType()==2){
                return ResponseResult.failed("菜单Url不能重复！");
            }else if(rbacMenu.getMenuType()==3){
                return ResponseResult.failed("权限表示不能重复！");
            }
        }
        return ResponseResult.ok(rbacMenuService.save(rbacMenu),"保存成功");
    }

    /**
     * 修改权限信息
     * @param rbacMenu 权限信息
     * @return R
     */
    @ApiOperation(value = "修改权限信息", notes = "修改权限信息")
    @PutMapping
    public ResponseResult updateById(@RequestBody RbacMenu rbacMenu) {
        RbacMenu saveRbacMenu = rbacMenuService.getById(rbacMenu.getPkId());
        if(saveRbacMenu==null){
            return ResponseResult.failed("操作记录不存在");
        }
        saveRbacMenu = getBean(saveRbacMenu, rbacMenu);
        return ResponseResult.ok(rbacMenuService.updateById(saveRbacMenu), "修改成功");
    }

    /**
     * 通过id删除权限信息
     * @param pkId id
     * @return R
     */
    @ApiOperation(value = "通过id删除权限信息", notes = "通过id删除权限信息")
    @DeleteMapping("/{pkId}" )
    public ResponseResult removeById(@PathVariable Long pkId) {
        RbacMenu rbacMenu = new RbacMenu().setParentId(pkId);
        List<RbacMenu> list = rbacMenuService.list(Wrappers.query(rbacMenu));
        if(list.size()>0){
            return ResponseResult.failed("请先删除子节点");
        }
        return ResponseResult.ok(rbacMenuService.removeById(pkId));
    }

    public RbacMenu getBean(RbacMenu menu_db, RbacMenu menu_form){
        menu_db.setParentId(menu_form.getParentId());
        menu_db.setMenuType(menu_form.getMenuType());
        menu_db.setDisplayName(menu_form.getDisplayName());
        menu_db.setMenuName(menu_form.getMenuName());
        menu_db.setMenuUrl(menu_form.getMenuUrl());
        menu_db.setAuthorityKey(menu_form.getAuthorityKey());
        menu_db.setMenuIcon(menu_form.getMenuIcon());
        menu_db.setMenuStatus(menu_form.getMenuStatus());
        menu_db.setOpenType(menu_form.getOpenType());
        menu_db.setOpenType(menu_form.getOpenType());
        menu_db.setIsShow(menu_form.getIsShow());
        return menu_db;
    }
}
